package com.cal.utms.model.payment;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.cal.utms.action.user_maintenance.LoginAction;
import com.cal.utms.dao.maintenance.CustomerDaoImpl;
import com.cal.utms.dao.maintenance.NewFundDao;
import com.cal.utms.dao.payment.InvestmentDaoImpl;
import com.cal.utms.dao.payment.PledgeDaoImpl;
import com.cal.utms.dto.payment.InvestmentForPledge;
import com.cal.utms.dto.payment.PledgeDto;
import com.cal.utms.entity.Customer;
import com.cal.utms.entity.CustomerPledge;
import com.cal.utms.entity.CustomerTransactionLog;

public class PledgeModel {

	PledgeDaoImpl daoImpl = new PledgeDaoImpl();
	CustomerDaoImpl customerDaoImpl = new CustomerDaoImpl();
	NewFundDao fundDao = new NewFundDao();
	InvestmentDaoImpl investmentModel = new InvestmentDaoImpl();

	public List<PledgeDto> getInvestmentForPledge(int fundId, int customerId) {

		List<PledgeDto> dtos = new ArrayList<PledgeDto>();
		List<InvestmentForPledge> forPledges = daoImpl.getInvestmentByFund(fundId, customerId);
		for (InvestmentForPledge investmentForPledge : forPledges) {
			if (investmentForPledge.getCustomerInvestment().getStatus().equals("CONFIRM")) {
				PledgeDto pledgeDto = new PledgeDto();
				Customer customer = investmentForPledge.getCustomer();
				pledgeDto.setId(investmentForPledge.getCustomerInvestment().getId());
				pledgeDto.setCustomer(customer.getId());
				pledgeDto.setInvesCode(investmentForPledge.getCustomerInvestment().getInvenstmentCode());
				pledgeDto.setInvestment(investmentForPledge.getCustomerInvestment().getId());
				pledgeDto.setFundid(fundId);
				pledgeDto.setFundUnitVal(investmentForPledge.getFund().getUnitValue());
				pledgeDto.setTotalUnits(investmentForPledge.getTotalUnits());
				pledgeDto.setTotalAmount(investmentForPledge.getTotalUnits().multiply(investmentForPledge.getFund().getUnitValue()));
				dtos.add(pledgeDto);
			}
		}

		return dtos;
	}

	public void savePledge(String pledges, PledgeDto pledgeDto) {

		if (pledges != null) {
			pledges = "{ \"pledges\" : ".concat(pledges) + " }";
			List<CustomerPledge> customerPledges = new ArrayList<CustomerPledge>();
			List<CustomerTransactionLog> customerTransactionLogs = new ArrayList<CustomerTransactionLog>();
			try {
				JSONObject jsonObject = new JSONObject(pledges);
				JSONArray jArray = jsonObject.getJSONArray("pledges");
				for (int i = 0; i < jArray.length(); i++) {
					JSONObject jObj = jArray.getJSONObject(i);
					CustomerPledge customerPledge = new CustomerPledge();
					customerPledge.setCustomer(customerDaoImpl.getCustomerById(jObj.getInt("customer")));
					customerPledge.setDescription(jObj.getString("description"));
					customerPledge.setFund(fundDao.getById(jObj.getInt("fundid")));
					customerPledge.setCustomerInvestment(investmentModel.getInvesById(jObj.getInt("investment")));
					customerPledge.setPledgeUnits(BigDecimal.valueOf(jObj.getDouble("pledgeUnits")));
					customerPledge.setRecordDate(new Date());
					customerPledge.setStatus("PLEDGE");
					customerPledge.setUser(LoginAction.getCurrentLogingUser());

					CustomerTransactionLog customerTransactionLog = createPledgeTrasaction(customerPledge);
					customerTransactionLogs.add(customerTransactionLog);
					customerPledges.add(customerPledge);
				}

				Object object = daoImpl.savePledges(customerPledges, customerTransactionLogs);
				if (object instanceof Integer) {
					pledgeDto.setSuccess(true);
					pledgeDto.setResulteMsg("SUCCESS");
				} else {
					pledgeDto.setSuccess(false);
					pledgeDto.setResulteMsg("There is some error returned from the server.");
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

	}

	private CustomerTransactionLog createPledgeTrasaction(CustomerPledge customerPledge) {
		CustomerTransactionLog customerTransactionLog = new CustomerTransactionLog();
		customerTransactionLog.setCustomer(customerPledge.getCustomer());
		customerTransactionLog.setFund(customerPledge.getFund());
		customerTransactionLog.setTranType("PLEDGE");
		customerTransactionLog.setCustomerInvestment(customerPledge.getCustomerInvestment());
		customerTransactionLog.setTranUnits(customerPledge.getPledgeUnits().negate());
		customerTransactionLog.setUser(LoginAction.getCurrentLogingUser());
		return customerTransactionLog;
	}

	private CustomerTransactionLog createPledgeTrasactionRelease(CustomerPledge customerPledge) {
		CustomerTransactionLog customerTransactionLog = new CustomerTransactionLog();
		customerTransactionLog.setCustomer(customerPledge.getCustomer());
		customerTransactionLog.setFund(customerPledge.getFund());
		customerTransactionLog.setTranType("PLEDGE");
		customerTransactionLog.setCustomerInvestment(customerPledge.getCustomerInvestment());
		customerTransactionLog.setTranUnits(customerPledge.getPledgeUnits());
		customerTransactionLog.setUser(LoginAction.getCurrentLogingUser());
		return customerTransactionLog;
	}

	public List<PledgeDto> getPledge(int fundId, int customerId) {
		List<CustomerPledge> customerPledges = daoImpl.getPledge(fundId, customerId);
		List<PledgeDto> pledgeDtos = new ArrayList<PledgeDto>();
		for (CustomerPledge customerPledge : customerPledges) {
			PledgeDto dto = new PledgeDto();
			dto.setId(customerPledge.getId());
			dto.setCustomer(customerPledge.getCustomer().getId());
			dto.setPledgeUnits(customerPledge.getPledgeUnits());
			dto.setFundid(customerPledge.getFund().getId());
			dto.setDescription(customerPledge.getDescription());
			dto.setInvestment(customerPledge.getCustomerInvestment().getId());
			dto.setInvesCode(customerPledge.getCustomerInvestment().getInvenstmentCode());
			pledgeDtos.add(dto);
		}
		return pledgeDtos;
	}

	public void releaseUnits(PledgeDto pledgeDto, int pledgeid) {
		CustomerPledge customerPledge = daoImpl.getPladgeByid(pledgeid);
		List<CustomerTransactionLog> customerTransactionLogs = new ArrayList<CustomerTransactionLog>();
		if (customerPledge != null) {

			CustomerTransactionLog customerTransactionLog = createPledgeTrasactionRelease(customerPledge);
			customerTransactionLogs.add(customerTransactionLog);

			customerPledge.setStatus("RELEASED");
			customerPledge.setReleaseDate(new Date());
			Object object = daoImpl.updatePladge(customerPledge, customerTransactionLogs);
			if (object instanceof CustomerPledge) {
				pledgeDto.setSuccess(true);
				pledgeDto.setResulteMsg("Success");
			} else {
				pledgeDto.setSuccess(false);
				pledgeDto.setResulteMsg("There is some error returned from the server.");
			}
		}
	}

}
